/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.GUI.sidepanels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.HashMap;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import model.graphic.GraphicModel;
import model.graphic.objects.GNode;
import model.graphic.objects.Graphical;
import view.GUI.LabelReader;
import view.GUI.actions.DebugAction;
import view.GUI.actions.DebugredAction;
import view.GUI.actions.RunAction;
import view.GUI.graphic.GraphicView;

/**
 *
 * @author Arfeniel
 */
public class RunPanel extends JPanel {

    private GraphicModel gmodel;
    private GraphicView gview;
    private HashMap<JLabel, GNode> mapping;
    private Object[][] data;
    private JButton btnRun;
    private JPanel jp;
    private JButton btnDebug;
    private JButton btnDebugred;
    private JTextArea debug;

    public RunPanel(GraphicView gview, GraphicModel gmodel, JTextArea debug) {
        this.mapping = new HashMap<JLabel, GNode>();
        this.setLayout(new BorderLayout());
        this.setBackground(Color.WHITE);
        this.gmodel = gmodel;
        this.gview = gview;
        btnRun = new JButton(new RunAction(gview, gmodel));
        btnRun.setIcon(new ImageIcon("icons\\run.png"));
        btnRun.setToolTipText(LabelReader.getInstance().getString("Run"));
        btnRun.setEnabled(true);
        btnRun.setMaximumSize(new Dimension(24, 24));
        btnRun.setPreferredSize(new Dimension(24, 24));
        btnDebug = new JButton(new DebugAction(gview, gmodel));
        btnDebug.setIcon(new ImageIcon("icons\\debug.png"));
        btnDebug.setToolTipText(LabelReader.getInstance().getString("DebugPopupTracker"));
        btnDebug.setEnabled(true);
        btnDebug.setMaximumSize(new Dimension(24, 24));
        btnDebug.setPreferredSize(new Dimension(24, 24));
        btnDebugred = new JButton(new DebugredAction(gview, gmodel, debug));
        btnDebugred.setIcon(new ImageIcon("icons\\debugred.png"));
        btnDebugred.setToolTipText(LabelReader.getInstance().getString("DebugHighLightTracker"));
        btnDebugred.setEnabled(true);
        btnDebugred.setMaximumSize(new Dimension(24, 24));
        btnDebugred.setPreferredSize(new Dimension(24, 24));

        jp = new JPanel();
        jp.setLayout(new FlowLayout(FlowLayout.RIGHT));
        jp.setBackground(Color.WHITE);
        jp.add(btnRun);
        jp.add(btnDebug);
        jp.add(btnDebugred);
    }

    public void updateRunButtons(boolean b) {
        btnRun.setEnabled(b);
        btnDebug.setEnabled(b);
        btnDebugred.setEnabled(b);

    }

    public void createStoresTable() {
        this.removeAll();
        mapping.clear();
        JLabel lab;
        for (Graphical g : gmodel.getAllNodes()) {
            if (GNode.class.isAssignableFrom(g.getClass())) {
                if (((GNode) g).getValue() != null) {
                    GNode gn = (GNode) g;
                    lab = new JLabel(gn.getName());
                    mapping.put(lab, gn);
                }
            }
        }

        int aantal = mapping.size();
        int tel = 0;
        if (aantal > 0) {
            data = new Object[aantal][2];
            for (JLabel l : mapping.keySet()) {
                data[tel][0] = l;
                data[tel][1] = mapping.get(l).getValue();
                tel++;
            }
            this.setAutoscrolls(true);
            String[] columnNames = {"Name", "Value"};
            RunTable table = new RunTable(new RunTableModel(columnNames, data, mapping, gview));
            add(table, BorderLayout.NORTH);
            add(jp, BorderLayout.CENTER);
            this.updateUI();
        }

    }
}
